<?php 
	header("Content-Type: application/xml; charset=utf-8");
?>

<?php
//session_start();
global $lg;
include('./includes/config.php');
if(isset($_GET['lg'])){
	$lg = mysql_real_escape_string($_GET["lg"]);
	$_SESSION['lg'] = mysql_real_escape_string($_GET["lg"]);
}
else
	$lg = 'vn';
	
include('language/'.$lg.'.php');
define('TPL_DIR','templates/site/');
define('SRC_DIR','sources/site/');
define('CLASS_DIR','class/');
include("./includes/constant.php");
include("./class/info.class.php");
include("./includes/functions.php");
include("./includes/va_db.php");
$domain = $_SERVER['HTTP_HOST'];
global $db, $cat, $FullUrl;

$FullUrl = 'http://' . $_SERVER['HTTP_HOST'] . $FullUrl;

if(isset($_GET['catKey'])){
 $key = mysql_real_escape_string($_GET['catKey']);
 $sql = "SELECT name_vn, name_en, id FROM `categories` WHERE unique_key_$lg = '$key'";
 
 $cat = $db->getRow($sql);
 
 if($cat)
 {
  $catID = $cat['id'];
  
  $sql = "SELECT name_vn, name_en, short_vn, short_en, img, dated, unique_key_vn, unique_key_en, cid FROM `articles` WHERE cid = $catID order by id desc limit 0,20";
 }
 else
  $sql = "SELECT name_vn, name_en, short_vn, short_en, img, dated, unique_key_vn, unique_key_en, cid FROM `articles` WHERE cid in (SELECT c1.id FROM `categories` c1 WHERE c1.pid in (SELECT c2.id FROM `categories` c2)) order by id desc limit 0,20";
}
else{
 $sql = "SELECT name_vn, name_en, short_vn, short_en, img, dated, unique_key_vn, unique_key_en, cid FROM `articles` WHERE cid in (SELECT c1.id FROM `categories` c1 WHERE c1.pid in (SELECT c2.id FROM `categories` c2)) order by id desc limit 0,20";
}
//echo "[" . $_GET['catKey'] . "]";
$articles = $db->getAll($sql);

if($articles)
{
 ?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="<?php echo $domain; ?>" type="application/rss+xml" />
<title>RSS - <?=$cat['name_'.$lg]?$cat['name_'.$lg]:'Latest News'?>
</title>
<link>
<?=$FullUrl;?>
</link>
<description>
RSS - <?=$cat['name_'.$lg]?$cat['name_'.$lg]:'Latest News'?>
</description>
<language>
  <?=$lg?>
</language>
<? $contactMail = Info::getByName('contact mail'); ?>
<managingEditor>
  <?=$contactMail['content_vn']?>
</managingEditor>
<?	
		global $Logo;
		
		$sql = "SELECT img FROM `banner` WHERE active = 1 and cid = 118 LIMIT 1";
		
		$Logo = $db->getAll($sql);
		
		$i = 0;
?>
<image>
  <url>
    <?=$FullUrl . "/" . $Logo[$i]["img"];?>
  </url>
  <title>
  <?=$_SERVER['HTTP_HOST'];?>
  </title>
  <link>
  http://<?=$_SERVER['HTTP_HOST'];?>
  </link>
  <? 
  	$path = $FullUrl.'/'.$Logo[$i]["img"];
	
	if(file_exists($path)){
		$size = getimagesize('./' . $Logo[$i]["img"]); print('./' . $Logo[$i]["img"]); 
  ?>
  <width><?=$size[0]?></width>
  <height><?=$size[1]?></height>
  <?
  	}else{
  ?>
  	<width>0</width>
  <height>0</height>
  <? } ?>
</image>
<?php foreach($articles as $article)
 {
  ?>
<item>
<title><?php echo htmlspecialchars($article['name_'.$lg]); ?> </title>
<link>
<?=$FullUrl;?>/<?=GetArtLink($article)?>
</link>
<description> <?php echo htmlspecialchars(strip_tags($article['short_'.$lg]), ENT_COMPAT, 'UTF-8'); ?>
  <?php 
  if($article['img'])
  {
   ?>
  <![CDATA[ <br />
    <img alt="img" width="120"; height="90" src="<?=$FullUrl?>/<?=GetImage($article['img']);?>" style="float:left; margin-right:10px; padding:2px;border:1px solid #999;" /> ]]>
  <?php 
  }
  ?>
</description>
<pubDate><?php print date("D, d M Y H:i:S", strtotime($article['dated']));?> +0700</pubDate>
</item>
<?php } ?>
</channel>
</rss>
<?php } ?>
